﻿#include "rightdockwindow.h"

extern void setButtonStyle(QPushButton *pushButton, const QString &imageUrl1, const QString &imageUrl2);
RightDockWindow::RightDockWindow(QWidget *parent, const Qt::AlignmentFlag &v_alignment, const Qt::AlignmentFlag &h_alignment, const QPoint &pos)
: StackWindow(parent, v_alignment, h_alignment, pos)
{
    //----------initalize widget
    widget=new QWidget(this);
    width=300;height=500;sideButtonWidth=25;sideButtonHeight=30;
    widget->setFixedSize(width-sideButtonWidth,height);
    this->setFixedSize(width,height);

    //Mouse penetrate
     widget->move(sideButtonWidth,0);
    mouseFilter=new MouseFilter(this);
    this->installEventFilter(mouseFilter);


    rightSideButton=new QPushButton(this);
    rightSideButton->setFixedSize(sideButtonWidth,sideButtonHeight );
    rightSideButton->move(0, 0);
    isShow = false;
    // 设置按钮样式跟随侧边栏状态而变化
    changeRightButtonStyle(false);
    //直接移动侧边栏无效，可能是界面重绘不支持。所以间隔0.2秒后再移动
    QTimer::singleShot(200, this, SLOT(slotMoveWidget()));
    //初始化动画
    anim1 = new QPropertyAnimation(this, "geometry");
    anim1->setEasingCurve(QEasingCurve::Linear);
    anim1->setDuration(500);

    connect(rightSideButton,&QPushButton::clicked,this,&RightDockWindow::rightSideButtonClicked);

    QVBoxLayout *layout = new QVBoxLayout(widget);
       QVBoxLayout *topLayout = new QVBoxLayout;
       QVBoxLayout *buttomLayout = new QVBoxLayout;
       QHBoxLayout *speedColumn = new QHBoxLayout;
       QHBoxLayout *v_directionColumn = new QHBoxLayout;
       QHBoxLayout *h_directionColumn = new QHBoxLayout;
       QHBoxLayout *xColumn = new QHBoxLayout;
       QHBoxLayout *yColumn = new QHBoxLayout;
       QHBoxLayout *zColumn = new QHBoxLayout;
       layout->addLayout(topLayout);
       layout->addLayout(buttomLayout);
       buttomLayout->addLayout(speedColumn);
       buttomLayout->addLayout(v_directionColumn);
       buttomLayout->addLayout(h_directionColumn);
       buttomLayout->addLayout(xColumn);
       buttomLayout->addLayout(yColumn);
       buttomLayout->addLayout(zColumn);
       topLayout->setMargin(25);
       layout->setSpacing(0);
       buttomLayout->setSpacing(10);
       speedLabel = new QLabel;
       v_directionLabel = new QLabel;
       h_directionLabel = new QLabel;
       xLabel = new QLabel;
       yLabel = new QLabel;
       zLabel = new QLabel;
       speedColumn->addWidget(new QLabel("speed: "));
       speedColumn->addWidget(speedLabel);
       v_directionColumn->addWidget(new QLabel("v_direction: "));
       v_directionColumn->addWidget(v_directionLabel);
       h_directionColumn->addWidget(new QLabel("h_direction: "));
       h_directionColumn->addWidget(h_directionLabel);
       xColumn->addWidget(new QLabel("x: "));
       xColumn->addWidget(xLabel);
       yColumn->addWidget(new QLabel("y: "));
       yColumn->addWidget(yLabel);
       zColumn->addWidget(new QLabel("z: "));
       zColumn->addWidget(zLabel);
       droneImage = new QLabel;
       droneImage->setFixedSize(200, 200);
   //    QPixmap dronePixmap = QPixmap(":/image/drone.png").scaled(200, 200, Qt::KeepAspectRatio);
   //    droneImage->setPixmap(dronePixmap);
       topLayout->addWidget(droneImage);
       //set the layout alignment
           //layout->setAlignment(_v_alignmentFlag | _h_alignmentFlag);
       layout->setAlignment(Qt::AlignLeft);
       widget->setVisible(true);
           widget->setLayout(layout);
           //widget->setAttribute(Qt::WA_TransparentForMouseEvents, true);
           widget->setStyleSheet("background-color: rgba(211, 211, 211, 0.3);");//set background color

           Drone * drone = new Drone(QPixmap(":/image/drone.png"), 300, 2, 3, 100, 200, 400);
           loadDrone(drone);
}
void RightDockWindow::changeRightButtonStyle(bool isShowSide)
{
    QString qss;
    if(!isShowSide) {
        qss = "QPushButton {border-image: url(:/image/ic_left_normal.png);border-radius: 6px;}"
                "QPushButton:hover {border-image: url(:/image/ic_left_normal.png);}"
                "QPushButton:pressed {border-image: url(:/image/ic_left_pressed.png);}";
    } else {
        qss = "QPushButton {border-image: url(:/image/ic_right_normal.png);border-radius: 6px;}"
                "QPushButton:hover {border-image: url(:/image/ic_right_normal.png);}"
                "QPushButton:pressed {border-image: url(:/image/ic_right_pressed.png);}";
    }
    this->rightSideButton->setStyleSheet(qss);
}
void RightDockWindow::rightSideButtonClicked()
{
    // 侧边栏和按钮动画
    if(isShow) {
            isShow = false;
            anim1->setStartValue(QRect(this->pos().x(), 100, width, height));
              anim1->setEndValue(QRect(this->pos().x()+width-sideButtonWidth, 100, width, height));

            //anim2->setStartValue(QRect(0, 0, 25, 25));
            //anim2->setEndValue(QRect(width-sideButtonWidth, 0, 25, 25));
        } else {
            isShow = true;
            anim1->setStartValue(QRect(this->pos().x(), 100, width, height));
            anim1->setEndValue(QRect(this->pos().x()-width+sideButtonWidth, 100, width, height));

            //anim2->setStartValue(QRect(width-sideButtonWidth, 0, 25, 25));
            //anim2->setEndValue(QRect(0, 0, 25, 25));
        }
    anim1->start();
    //anim2->start();

    changeRightButtonStyle(isShow);
}

void RightDockWindow::slotMoveWidget()
{
    widget->move(widget->width(), 0);
}

void RightDockWindow::loadDrone(Drone *newDrone)
{
    this->droneImage->setPixmap(newDrone->pixmap);
    this->speedLabel->setText(QString::number(newDrone->speed));
    this->v_directionLabel->setText(QString::number(newDrone->v_direction));
    this->h_directionLabel->setText(QString::number(newDrone->h_direction));
    this->xLabel->setText(QString::number(newDrone->x));
    this->yLabel->setText(QString::number(newDrone->y));
    this->zLabel->setText(QString::number(newDrone->z));
}
